package com.auto.survey.activity.monitoring.project

import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.adapter.MontoringAdapter
import com.auto.survey.activity.monitoring.dao.MonitoringRepository
import com.auto.survey.base.BaseActivity
import com.auto.survey.util.Helper
import com.auto.survey.widget.CommonTitle.Companion.TEXT_POSITON_RIGHT
import kotlinx.coroutines.launch

/**
 * 自动化监测
 */
class ManagerProjectActivity : BaseActivity() {



    private val database = MonitoringRepository()
    private val adapter  = MontoringAdapter(this)

    override fun initView() {
        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("自动化监测项目管理")

        mComment_title?.setRightTitle("新建项目")
        mComment_title?.setTitleColor(Color.WHITE, TEXT_POSITON_RIGHT)
        mComment_title?.setOnRightTextClickListener(Helper.debounce{

            val intent = Intent()
            intent.setClass(this@ManagerProjectActivity, CreateProjectActivity::class.java)
            startActivity(intent)

        })

        val rv_ats = findViewById<RecyclerView>(R.id.rv_ats_moto)
        rv_ats.layoutManager = LinearLayoutManager(this@ManagerProjectActivity)
        rv_ats.adapter = adapter

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            database.getProjectList().collect {
                adapter.setItems(it)
            }
        }

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_ats_montoring
    }
}